package com.tencent.externalstoragebar.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.tencent.externalstoragebar.MainActivity;
import com.tencent.externalstoragebar.evetntools.AnalyticData;
import com.tencent.externalstoragebar.evetntools.GlobalTouchListener;
import com.tencent.externalstoragebar.tools.l;

public class GetSlotService extends Service implements GlobalTouchListener {

    //最终给AnalyticData的cmd命令
    public static String finalCmd="getevent -l /dev/input/event";

    //执行命令
    public static String cmd = "getevent";

    //是否结束
    public static boolean over = false;

    //重写action方法
    @Override
    public void action() {
        l.d("action");
        new Thread(new Runnable() {
            @Override
            public void run() {
                execShellCmd(cmd);
            }
        }).start();
    }
    @Override
    public void usageData(String line) {
        //拿到数据后干嘛
        //如果出现
        //   /dev/input/eventX: 0000 0000 00000000
        //则表示X为槽位
        //记录槽位
        l.d(line);
        if(line.contains("0000 0000 00000000")){

            //按照/dev/input/event来切割
            String[] split = line.split("/dev/input/event");

            //获取槽位
            String slot = split[1].charAt(0)+"";

            //打印日志测试
            l.d(slot+"是槽位");

            //将槽位更新到服务槽位中
            AnalyticData.slot=slot;
            AnalyticData.cmd=finalCmd+slot;

            //一旦获取槽位,就可以停止当前服务了
            //标志结束
            over = true;

            //停止服务
            l.d("停止获取槽位的服务");
            stopSelf();

            //启动
            Class<MainActivity> mainActivityClass = MainActivity.class;
        }

    }


    //服务启动方法
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        l.d("GetSlotService onBind");
        action();
        return super.onStartCommand(intent, flags, startId);
    }

    //结束方法
    //返回true表示结束
    @Override
    public boolean over() {
        if (over){
            l.d("测试出槽位,结束<获取槽位服务的监听线程>");
        }
        return over;
    }

    //不管的方法
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
